package me.zhengjie.modules.bailian.rest;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.rest.AnonymousAccess;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.handler.ApiError;
import me.zhengjie.modules.bailian.domain.EosCustomerInfoPreLog;
import me.zhengjie.modules.bailian.service.EosCustomerInfoPreLogService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Aliyun百炼插件服务 2025/4/24 15:59
 *
 * @author Yaco
 * @version 1.0.0
 */

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "Aliyun百炼插件服务")
@RequestMapping("/bailian_plugin")
public class BailianPluginController {

    public static final String API_KEY = "3f1e2d4c5b6a798234567890abcdef1234567890abcdef12345678";

    private final EosCustomerInfoPreLogService eosCustomerInfoPreLogService;

    @AnonymousAccess
    @ApiOperation("EOS_USERS插件")
    @PostMapping(value = "/addEosUsers")
    public ResponseEntity<Object> addEosUsers(@RequestParam("Authorization") String authorization, @RequestBody EosCustomerInfoPreLog resources) {
        if (StrUtil.isBlank(authorization) || !authorization.equals(API_KEY)) {
            throw new BadRequestException(HttpStatus.UNAUTHORIZED, "Missing parameter API_KEY");
        }
        if (StrUtil.isAllBlank(resources.getUserName(), resources.getUserEmail(), resources.getUserPhone(), resources.getCompanyName())) {
            log.info(" ### resources is null");
            return new ResponseEntity<>(ApiError.error(HttpStatus.NO_CONTENT.value(), "No operation required"), HttpStatus.CREATED);
        } else {
            resources.setRecordStatus(1);
            eosCustomerInfoPreLogService.create(resources);
            return new ResponseEntity<>(ApiError.error(HttpStatus.CREATED.value(), "Success"), HttpStatus.CREATED);
        }
    }
}
